package com.amtf.datafile3.core.excel;

import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.amtf.datafile3.DataException;
import com.amtf.datafile3.DataTable;
import com.amtf.datafile3.DataTableDefinition;
import com.amtf.datafile3.cfg.excel.ExcelDataDefinitionFactory;
import com.amtf.datafile3.cfg.excel.ExcelDataTableDefinition;
import com.amtf.datafile3.core.std.AbsDataCatalog;

public class ExcelDataCatalog extends AbsDataCatalog {
	private Workbook workbook = null;
	// private boolean firstRowTitle;
	// private int startReadRow = 0;
	// private int startReadColumn = 0;
	// private int maxReadRow = 1048576;// 65535;
	// private int maxReadColumn = 16384;// 256;
	// private int startWriteRow = 0;
	// private int startWriteColumn = 0;
	private FormulaEvaluator evaluator;

	public ExcelDataCatalog(String name, Workbook workbook, boolean firstRowTitle, int startReadRow, int startReadColumn, int startWriteRow, int startWriteColumn)
			throws DataException {
		super(name);

		if (startReadRow <= 1) {
			startReadRow = 0;
		} else {
			startReadRow -= 1;
		}
		if (startReadColumn <= 1) {
			startReadColumn = 0;
		} else {
			startReadColumn -= 1;
		}

		this.workbook = workbook;
		// this.firstRowTitle = firstRowTitle;
		// this.startReadRow = startReadRow;
		// this.startReadColumn = startReadColumn;
		// this.startWriteRow = startWriteRow;
		// this.startWriteColumn = startWriteColumn;

		this.evaluator = workbook.getCreationHelper().createFormulaEvaluator();
		int size = workbook.getNumberOfSheets();
		for (int i = 0; i < size; i++) {
			Sheet sheet = workbook.getSheetAt(i);
			if (sheet == null) {
				throw new DataException("Could not get sheet at index " + i);
			}

			DataTableDefinition tabledef = ExcelDataDefinitionFactory.create(sheet.getSheetName(), firstRowTitle, startReadRow, startReadColumn,
					startWriteRow, startWriteColumn, evaluator);
			ExcelDataTable dt = new ExcelDataTable(sheet.getSheetName(), tabledef, sheet, evaluator);
			setDataTable(sheet.getSheetName(), dt);
		}
	}

	public Workbook getWorkbook() {
		return workbook;
	}

	public DataTable createDataTable(String name, DataTableDefinition definition) throws DataException {
		((ExcelDataTableDefinition) definition).setEvaluator(evaluator);

		Sheet sheet = workbook.createSheet(name);
		DataTable workSheet = this.getDataTable(name);
		if (workSheet == null) {
			workSheet = new ExcelDataTable(name, definition, sheet, evaluator);
			setDataTable(name, workSheet);
		}

		return workSheet;
	}

	@Override
	public DataTable dropDataTable(String name) throws DataException {
		DataTable dt = super.getDataTable(name);
		if (dt != null) {
			dt.close();
			int index = workbook.getSheetIndex(name);
			if (index > 0) {
				workbook.removeSheetAt(index);

				return super.dropDataTable(name);
			}
		}
		return null;
	}
}
